//
//  KKUserInfoViewController.m
//  ReplyAssistant
//
//  Created by Leo on 2018/11/24.
//  Copyright © 2018 KK. All rights reserved.
//

#import "KKUserInfoViewController.h"

#import "KKNetworkRequest+V1.h"

#import "KKImagePickerManager.h"
#import "KKTopAlertView.h"

@interface KKUserInfoViewController ()

@end

@implementation KKUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"设置";
    [self private_updateUI];
    [self private_getUserInfo];
}

- (void)loadView {
    [super loadView];
    
    self.img_headimg.layer.cornerRadius = CGRectGetHeight(self.img_headimg.bounds)/2.0;
    self.img_headimg.layer.masksToBounds = true;
    
    self.img_headimg.layer.borderColor = [UIColor colorWithRed:61/255.0 green:83/255.0 blue:114/255.0 alpha:0.8].CGColor;
    self.img_headimg.layer.borderWidth = 5;
    
    self.btn_exit.layer.cornerRadius = CGRectGetHeight(self.btn_exit.bounds)/2.0;
    self.btn_exit.layer.borderColor = [UIColor colorWithHexString: @"85b2f6"].CGColor;
    self.btn_exit.layer.borderWidth = 2;
    
}

#pragma mark -

- (void)private_getUserInfo{
    
    __weak typeof(self) weakSelf = self;
    
    KKNetworkRequest *request = [KKNetworkRequest request_getUserInfo];
    [KKNetworking sendRequest: request responseCallBack:^(id response, NSString *error) {
        if ([response isKindOfClass: [NSDictionary class]]) {
            //  解析会话对象列表
            [KKUser loadingUserDefailInfoWithJson: response];
        }
        else {
            [weakSelf kk_showTopAlertViewWithMessage: error];
        }
        [weakSelf private_updateUI];
    }];
}

- (void)private_updateUI {
    NSString *headimagePath = KKUser.defaultUser.headimg;
    [self.img_headimg setImageWithURL: [NSURL URLWithString: headimagePath] placeholderImage: nil];
    self.lab_nickname.text = KKUser.defaultUser.nickName;
    self.lab_messageNumber.text = [NSString stringWithFormat: @"%@",@(KKUser.defaultUser.message)];
    self.lab_customerNumber.text = [NSString stringWithFormat: @"%@",@(KKUser.defaultUser.customer)];
    self.lab_company.text = [NSString stringWithFormat: @"@%@",KKUser.defaultUser.team];
    self.online.on = KKUser.defaultUser.online;
}

#pragma mark - Action methods
- (IBAction)action_online:(UISwitch*)sender {
    
    KKNetworkRequest *request = [KKNetworkRequest request_updateServiceStatus: sender.on];
    [KKNetworking sendRequest: request showStatus: true responseCallBack:^(id response, NSString *error) {
        
    }];
}

- (IBAction)action_exit:(id)sender {
    [UIAlertController kk_alertShowTitle: @"提示" message: @"退出后需重新扫描二维码登录，确认退出？" cancelTitle: @"取消" cancelHandle: nil sureTitle: @"确认" sureHandle:^{
        [KKUser exit];
    }];
}

- (IBAction)action_updateHeadimg:(id)sender {
    
    __weak typeof(self) weakSelf = self;
    KKImagePickerManager *manager = [KKImagePickerManager kk_defaultManager];
    manager.maxLimit = 1;
    manager.edit = true;
    manager.resultBlock = ^(NSArray *images) {
        weakSelf.img_headimg.image = [images firstObject];
        [weakSelf private_sendUploadImageRequest];
    };
    [manager kk_takePicture];
}

#pragma mark -
- (void)private_sendUploadImageRequest {
    
    __weak typeof(self) weakSelf = self;
    [KKNetworking uploadImage: self.img_headimg.image parmeters: nil progress: nil responseCallBack:^(id response, NSString *error) {
        if ([response isKindOfClass: [NSString class]]) {
            //  上传成功
            NSString *imgPath = (NSString*)response;
            [weakSelf private_sendLoginRequestWithImagePath: imgPath];
        }
        else {
            [weakSelf kk_showTopAlertViewWithMessage: error];
        }
    }];
}

- (void)private_sendLoginRequestWithImagePath:(NSString*)imagePath {
 
    KKNetworkRequest *request = [KKNetworkRequest request_updateUserHeadimg: imagePath];
    [KKNetworking sendRequest: request showStatus: false responseCallBack:^(id response, NSString *error) {
    }];
}

@end
